-- Create a table for public profiles
create table profiles(
  id uuid references auth.users not null primary key,
  updated_at timestamp with time zone,
  username text unique,
  full_name text,
  avatar_url text,
  website text,
  constraint username_length check (char_length(username) >= 3)
);

-- Set up Row Level Security (RLS)
-- See https://supabase.com/docs/guides/auth/row-level-security for more details.
alter table profiles enable row level security;

create policy "Public profiles are viewable by everyone." on profiles
  for select
    using (true);

create policy "Users can insert their own profile." on profiles
  for insert
    with check (auth.uid() = id);

create policy "Users can update own profile." on profiles
  for update
    using (auth.uid() = id);

-- This trigger automatically creates a profile entry when a new user signs up via Supabase Auth.
-- See https://supabase.com/docs/guides/auth/managing-user-data#using-triggers for more details.
create function public.handle_new_user()
  returns trigger
  as $$
begin
  insert into public.profiles(id, full_name, avatar_url)
    values(new.id, new.raw_user_meta_data ->> 'full_name', new.raw_user_meta_data ->> 'avatar_url');
  return new;
end;
$$
language plpgsql
security definer;

create trigger on_auth_user_created
  after insert on auth.users for each row
  execute procedure public.handle_new_user();

-- Set up Storage!
insert into storage.buckets(id, name)
  values ('avatars', 'avatars');

-- Set up access controls for storage.
-- See https://supabase.com/docs/guides/storage/security/access-control#policy-examples for more details.
create policy "Give users access to own folder 1oj01fe_0" on storage.objects
  for select to authenticated
    using (bucket_id = 'avatars'
      and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));

create policy "Give users access to own folder 1oj01fe_1" on storage.objects
  for insert to authenticated
    with check (bucket_id = 'avatars'
    and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));

create policy "Give users access to own folder 1oj01fe_2" on storage.objects
  for update to authenticated
    using (bucket_id = 'avatars'
      and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));

create policy "Give users access to own folder 1oj01fe_3" on storage.objects
  for delete to authenticated
    using (bucket_id = 'avatars'
      and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));

